package com.quanshu.exchange.ui.gui.message.group;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.quanshu.exchange.R;
import com.quanshu.exchange.support.db.GroupDao;
import com.quanshu.exchange.support.entity.GroupEntity;
import com.quanshu.exchange.support.entity.tmp.TmpDestChatUser;
import com.quanshu.exchange.support.utils.CheckUtil;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.recycler.BaseViewHolder;
import com.quanshu.exchange.ui.base.recycler.DataDisplayer;
import com.quanshu.exchange.ui.base.recycler.LayoutWrapper;
import com.quanshu.exchange.ui.base.recycler.SingleAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 群发消息 - 选择群组 - 我的群组
 * Created by xiniu003 on 2017/12/12.
 */

public class SelectGroupActivity extends BaseActivity {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.ok_btn)
    ImageView okBtn;
    private SingleAdapter<GroupEntity> adapter;
    private List<TmpDestChatUser> beSelectedList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_group);
        ButterKnife.bind(this);
        initIntent();
        initView();
        initData();
        initListener();
    }

    private void initIntent() {
        beSelectedList = (List<TmpDestChatUser>) getIntent().getSerializableExtra("List<TmpDestChatUser>");
    }

    private void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SingleAdapter<>(new DataDisplayer<GroupEntity>(this, R.layout.item_select_group_layout) {
            @Override
            public void bindData(BaseViewHolder holder, LayoutWrapper<GroupEntity> wrapper, GroupEntity item, int position) {
                holder.setRoundImageUrl(R.id.avatar, item.getGroup_photo(), R.drawable.default_group_avatar);
                holder.setText(R.id.name, item.getGroup_name());
                ImageView checkBox = holder.getView(R.id.group_check);
                if (CheckUtil.isValidate(beSelectedList)) {
                    for (TmpDestChatUser chatUser : beSelectedList) {
                        if (chatUser.getDestId().equals(item.getHx_groupid())) {
                            checkBox.setSelected(true);
                            break;
                        } else {
                            checkBox.setSelected(false);
                        }
                    }
                } else {
                    checkBox.setSelected(false);
                }
                checkBox.setOnClickListener(view -> {
                    boolean status = checkBox.isSelected();
                    if (beSelectedList == null) {
                        beSelectedList = new ArrayList<>();
                    }
                    if (beSelectedList.size() > 10) {
                        ToastHelper.toast("最多可发送10个对象");
                        return;
                    }
                    if (!status) {
                        TmpDestChatUser chatUser = new TmpDestChatUser();
                        chatUser.setDestId(item.getHx_groupid());
                        chatUser.setAvatar(item.getGroup_photo());
                        chatUser.setGroupOwner(item.getOwner());
                        chatUser.setFlag(1);
                        chatUser.setVisible(item.getVisible());
                        chatUser.setRelation_type(item.getRelation_type());
                        chatUser.setTitle(item.getGroup_name());
                        beSelectedList.add(chatUser);
                    } else {
                        Iterator<TmpDestChatUser> it = beSelectedList.iterator();
                        while (it.hasNext()) {
                            if (it.next().getDestId().equals(item.getHx_groupid())) {
                                it.remove();
                                break;
                            }
                        }
                    }
                    checkBox.setSelected(!status);
                    adapter.notifyDataSetChanged();
                });
            }
        });
        recyclerView.setAdapter(adapter);
    }

    private void initData() {

        List<GroupEntity> groupEntities = GroupDao.getInstance().queryForAll();
        adapter.setData(groupEntities);
    }

    private void initListener() {

        RxViewHelper.getInstance().clicks(back, aVoid -> finish());
        RxViewHelper.getInstance().clicks(okBtn, aVoid -> doOk());
    }

    private void doOk() {
        if (beSelectedList == null || beSelectedList.size() < 1) {
            ToastHelper.toast("还未选择群组");
            return;
        }
        Intent intent = getIntent();
        intent.putExtra("List<TmpDestChatUser>", (Serializable) beSelectedList);
        setResult(RESULT_OK, intent);
        finish();
    }
}
